﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ZhCun.Utils;
using ZhCun.WebUtils.Authorizes;
using ZhCun.WebUtils.DB;

namespace ZhCun.WebUtils
{
    /// <summary>
    /// api 控制器基类
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [UserAuthorize]
    [BodyEncryptFilter]
    public class BaseApiController : ControllerBase, ILoginSession, IApiResultExtend
    {
        #region ILoginSession

        LoginUser _CurrUser;

        public LoginUser CurrUser
        {
            get
            {
                if (_CurrUser == null)
                {
                    _CurrUser = this.GetCurrUser();
                }
                return _CurrUser;
            }
        }

        string _RemoteIp;
        /// <summary>
        /// 返回远程请求地址
        /// </summary>
        protected string RemoteIp
        {
            get
            {
                if (_RemoteIp == null)
                {
                    _RemoteIp = Request.HttpContext.GetRemoteIp();
                }
                return _RemoteIp;
            }
        }


        public string LoginHost
        {
            get
            {
                if (_RemoteIp == null)
                {
                    _RemoteIp = Request.HttpContext.GetRemoteIp();
                }
                return _RemoteIp;
            }
        }

        //public string CurrUserId
        //{
        //    get
        //    {
        //        return CurrUser?.UserId;
        //    }
        //}

        //public string CurrUserName
        //{
        //    get
        //    {
        //        return CurrUser?.UserName;
        //    }
        //}

        //public string CurrRoleId
        //{
        //    get
        //    {
        //        return CurrUser?.RoleId;
        //    }
        //}

        //public object Tag { set; get; }

        //public string CurrDeptId
        //{
        //    get
        //    {
        //        return CurrUser?.DeptId;
        //    }
        //}

        //public string CurrDeptName
        //{
        //    get
        //    {
        //        return CurrUser?.DeptName;
        //    }
        //}

        //public bool IsManager => CurrUser != null && CurrUser.IsManager;

        #endregion
    }

    public class BaseApiController<TBLLObj> : BaseApiController
        where TBLLObj : ILoginSessionSet, new()
    {
        TBLLObj _BLLObj;

        protected TBLLObj BLLObj
        {
            get
            {
                if (_BLLObj == null)
                {
                    _BLLObj = new TBLLObj();
                    _BLLObj.SetLoginSession(this);
                }
                return _BLLObj;
            }
        }
    }

}